package org.szgb.console.ht.electricity.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class HtElectricitySettlementBill extends BeanEntity<HtElectricitySettlementBill> {
    @Serial
    private static final long serialVersionUID = 1L;
    private HtElectricitySettlementBillModel htElectricitySettlementBillModel;
    @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM")
    private LocalDate month;
    private HtElectricityPrice htElectricityPrice;
    private BigDecimal sumPrice;
    private String sumRemark;

    public HtElectricitySettlementBill() {
    }

    public HtElectricitySettlementBill(String id) {
        this.setId(id);
    }

    public HtElectricitySettlementBill(HtElectricitySettlementBill.HtElectricitySettlementBillFindDto htElectricitySettlementBillFindDto) {
        this.setId(htElectricitySettlementBillFindDto.getId());
        this.setHtElectricitySettlementBillModel(new HtElectricitySettlementBillModel(htElectricitySettlementBillFindDto.getHtElectricitySettlementBillModelId()));
        if (htElectricitySettlementBillFindDto.getMonth() != null) {
            this.setMonth(htElectricitySettlementBillFindDto.getMonth().atDay(1));
        }
        this.setPageDto(htElectricitySettlementBillFindDto.getPageDto());
    }

    public HtElectricitySettlementBill(HtElectricitySettlementBill.HtElectricitySettlementBillSaveDto htElectricitySettlementBillSaveDto) {
        this.setId(htElectricitySettlementBillSaveDto.id);
        this.setHtElectricitySettlementBillModel(new HtElectricitySettlementBillModel(htElectricitySettlementBillSaveDto.htElectricitySettlementBillModelId));
        if (htElectricitySettlementBillSaveDto.month != null) {
            this.setMonth(htElectricitySettlementBillSaveDto.month.atDay(1));
        }
        this.setHtElectricityPrice(new HtElectricityPrice(htElectricitySettlementBillSaveDto.htElectricityPriceId));
        this.setSumRemark(htElectricitySettlementBillSaveDto.sumRemark);
    }

    public HtElectricitySettlementBill(HtElectricitySettlementBill.HtElectricitySettlementBillCreateDto htElectricitySettlementBillCreateDto) {
        this.setHtElectricitySettlementBillModel(new HtElectricitySettlementBillModel(htElectricitySettlementBillCreateDto.htElectricitySettlementBillModelId));
        this.setMonth(htElectricitySettlementBillCreateDto.month.atDay(1));
    }

    public HtElectricitySettlementBill.HtElectricitySettlementBillView toHtElectricitySettlementBillView() {
        return new HtElectricitySettlementBill.HtElectricitySettlementBillView(this);
    }

    public HtElectricitySettlementBill.HtElectricitySettlementBillShowView toHtElectricitySettlementBillShowView(List<HtElectricitySettlementBillItem.HtElectricitySettlementBillItemView> htElectricitySettlementBillItemViewList) {
        return new HtElectricitySettlementBill.HtElectricitySettlementBillShowView(this, htElectricitySettlementBillItemViewList);
    }

    @Data
    @Schema(title = "电量结算单信息", description = "电量结算单信息")
    public static class HtElectricitySettlementBillView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "电量结算单模板id")
        private String htElectricitySettlementBillModelId;
        @Schema(description = "电量结算单模板名称")
        private String htElectricitySettlementBillModelName;
        @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM")
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;
        @Schema(description = "电价id")
        private String htElectricityPriceId;
        @Schema(description = "电价")
        private BigDecimal htElectricityPrice;
        @Schema(description = "总价")
        private BigDecimal sumPrice;
        @Schema(description = "总价备注")
        private String sumRemark;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public HtElectricitySettlementBillView(HtElectricitySettlementBill htElectricitySettlementBill) {
            this.id = htElectricitySettlementBill.getId();
            this.htElectricitySettlementBillModelId = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getId();
            this.htElectricitySettlementBillModelName = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getName();
            if (htElectricitySettlementBill.getMonth() != null) {
                this.month = YearMonth.from(htElectricitySettlementBill.getMonth());
            }
            this.htElectricityPriceId = htElectricitySettlementBill.getHtElectricityPrice().getId();
            this.htElectricityPrice = htElectricitySettlementBill.getHtElectricityPrice().getPrice();
            this.sumPrice = htElectricitySettlementBill.getSumPrice();
            this.sumRemark = htElectricitySettlementBill.getSumRemark();
            this.createdAt = htElectricitySettlementBill.getCreatedAt();
            this.updatedAt = htElectricitySettlementBill.getUpdatedAt();
            if (htElectricitySettlementBill.getCreatedBy() != null) {
                this.createdById = htElectricitySettlementBill.getCreatedBy().getId();
                this.createdByName = htElectricitySettlementBill.getCreatedBy().getName();
            }
            if (htElectricitySettlementBill.getUpdatedBy() != null) {
                this.updatedById = htElectricitySettlementBill.getUpdatedBy().getId();
                this.updatedByName = htElectricitySettlementBill.getUpdatedBy().getName();
            }
        }

        public static List<HtElectricitySettlementBill.HtElectricitySettlementBillView> getViewListFromList(List<HtElectricitySettlementBill> htElectricitySettlementBillList) {
            return htElectricitySettlementBillList.stream().map(HtElectricitySettlementBill.HtElectricitySettlementBillView::new).toList();
        }
    }

    @Data
    @Schema(title = "电量结算单展示信息", description = "电量结算单展示信息")
    public static class HtElectricitySettlementBillShowView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "电量结算单模板id")
        private String htElectricitySettlementBillModelId;
        @Schema(description = "电量结算单模板名称")
        private String htElectricitySettlementBillModelName;
        @Schema(description = "电量结算单模板说明")
        private String htElectricitySettlementBillModelRemark;
        @Schema(description = "客户名称")
        private String customerName;
        @Schema(description = "项目名称")
        private String projectName;
        @Schema(description = "合同号与名称")
        private String contractNoAndName;
        @Schema(description = "甲方")
        private String partyAName;
        @Schema(description = "乙方")
        private String partyBName;
        @Schema(description = "丙方")
        private String partyCName;
        @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM")
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;
        @Schema(description = "电价id")
        private String htElectricityPriceId;
        @Schema(description = "电价")
        private BigDecimal htElectricityPrice;
        @Schema(description = "总价")
        private BigDecimal sumPrice;
        @Schema(description = "总价备注")
        private String sumRemark;
        @Schema(description = "结算单细项列表")
        private List<HtElectricitySettlementBillItem.HtElectricitySettlementBillItemView> htElectricitySettlementBillItemViewList;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public HtElectricitySettlementBillShowView(HtElectricitySettlementBill htElectricitySettlementBill,List<HtElectricitySettlementBillItem.HtElectricitySettlementBillItemView> htElectricitySettlementBillItemViewList) {
            this.id = htElectricitySettlementBill.getId();
            this.htElectricitySettlementBillModelId = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getId();
            this.htElectricitySettlementBillModelName = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getName();
            this.htElectricitySettlementBillModelRemark = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getRemark();
            this.customerName = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getCustomerName();
            this.projectName = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getProjectName();
            this.contractNoAndName = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getContractNoAndName();
            this.partyAName = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getPartyAName();
            this.partyBName = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getPartyBName();
            this.partyCName = htElectricitySettlementBill.getHtElectricitySettlementBillModel().getPartyCName();
            if (htElectricitySettlementBill.getMonth() != null) {
                this.month = YearMonth.from(htElectricitySettlementBill.getMonth());
            }
            this.htElectricityPriceId = htElectricitySettlementBill.getHtElectricityPrice().getId();
            this.htElectricityPrice = htElectricitySettlementBill.getHtElectricityPrice().getPrice();
            this.sumPrice = htElectricitySettlementBill.getSumPrice();
            this.sumRemark = htElectricitySettlementBill.getSumRemark();
            this.createdAt = htElectricitySettlementBill.getCreatedAt();
            this.updatedAt = htElectricitySettlementBill.getUpdatedAt();
            if (htElectricitySettlementBill.getCreatedBy() != null) {
                this.createdById = htElectricitySettlementBill.getCreatedBy().getId();
                this.createdByName = htElectricitySettlementBill.getCreatedBy().getName();
            }
            if (htElectricitySettlementBill.getUpdatedBy() != null) {
                this.updatedById = htElectricitySettlementBill.getUpdatedBy().getId();
                this.updatedByName = htElectricitySettlementBill.getUpdatedBy().getName();
            }
            this.htElectricitySettlementBillItemViewList=htElectricitySettlementBillItemViewList;
        }
    }

    @Data
    @Schema(title = "电量结算单查询表单", description = "电量结算单查询参数")
    public static class HtElectricitySettlementBillFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "电量结算单模板id")
        private String htElectricitySettlementBillModelId;
        @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM")
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public HtElectricitySettlementBill toHtElectricitySettlementBill() {
            return new HtElectricitySettlementBill(this);
        }
    }

    @Data
    @Schema(title = "电量结算单保存表单", description = "电量结算单保存参数")
    public static class HtElectricitySettlementBillSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "电量结算单模板id")
        private String htElectricitySettlementBillModelId;
        @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM")
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;
        @Schema(description = "电价id")
        private String htElectricityPriceId;
        @Schema(description = "总价备注")
        private String sumRemark;

        public HtElectricitySettlementBill toHtElectricitySettlementBill() {
            return new HtElectricitySettlementBill(this);
        }
    }

    @Data
    @Schema(title = "电量结算单创建表单", description = "电量结算单创建参数")
    public static class HtElectricitySettlementBillCreateDto {
        @Schema(description = "电量结算单模板id")
        private String htElectricitySettlementBillModelId;
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;

        public HtElectricitySettlementBill toHtElectricitySettlementBill() {
            return new HtElectricitySettlementBill(this);
        }
    }
}
